
//
//  MOPlaceholder.m
//  WCImApp
//
//  Created by alfie on 2019/11/19.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "MOPlaceholder.h"
#import "MOPlaceholderView.h"

@interface MOPlaceholder ()

/** MOPlaceholderOptions */
@property (assign, nonatomic) MOPlaceholderOptions option;

/** 控制器 */
@property (weak, nonatomic) id                     target;

/** placeholderView */
@property (strong, nonatomic) MOPlaceholderView    *placeholderView;

/** 刷新方法回调 */
@property (copy, nonatomic) MOPlaceholderBlock     refreshAction;

/** 登录方法回调 */
@property (copy, nonatomic) MOPlaceholderBlock     loginAction;

/** frame */
@property (assign, nonatomic) CGRect               frame;

///** 约束 */
//@property (strong, nonatomic) NSMutableArray       *conditions;
@end


@implementation MOPlaceholder
#pragma mark -- 生命周期
- (instancetype)init {
    if (self = [super init]) {
        self.option = MOPlaceholderOptionPrepare;
//        self.automaticStatus = YES;
    }
    return self;
}



#pragma mark -- UI
- (MOPlaceholderView *)placeholderView {
    if (!_placeholderView) {
        _placeholderView = [[MOPlaceholderView alloc] init];
        _placeholderView.backgroundColor = UIColor.whiteColor;
        [_placeholderView.reloadBtn addTarget:self action:@selector(btnAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _placeholderView;
}



#pragma mark -- 配置
- (instancetype)makeTarget:(id)target {
    self.target = target;
    return self;
}

- (instancetype)makeRefreshHandler:(MOPlaceholderBlock)refreshHandler {
    self.refreshAction = refreshHandler;
    return self;
}

- (instancetype)makeLoginHandler:(MOPlaceholderBlock)loginHandler {
    self.loginAction = loginHandler;
    return self;
}

//- (instancetype)makeFrame:(CGRect)frame {
//    self.frame = frame;
//    return self;
//}



#pragma mark -- 显示/隐藏
//- (BOOL)isLoading {
//    return (BOOL)(self.option == MOPlaceholderOptionLoading);
//}

- (void)loading {
    [self showOption:(MOPlaceholderOptionLoading)];
}

- (void)hide {
    [self showOption:(MOPlaceholderOptionNone)];
}


- (void)showOption:(MOPlaceholderOptions)option {
    
    if (self.option == option) {
        return;
    }
    
//    static dispatch_semaphore_t semaphore;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        semaphore = dispatch_semaphore_create(1);
//    });
//    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    UIView *superView;
    if ([_target isKindOfClass:[UIViewController class]]) {
        superView = [(UIViewController *)_target view];
    } else if ([_target isKindOfClass:[UIView class]]) {
        superView = _target;
    } else {
        superView = [UIApplication sharedApplication].keyWindow;
    }
    
    self.option = option;
    self.placeholderView.frame = CGRectEqualToRect(_frame, CGRectZero) ? CGRectMake(0, 0, superView.frame.size.width, superView.frame.size.height) : _frame;
    
    switch (option) {
        case MOPlaceholderOptionNone:
            //隐藏
            if (_placeholderView.superview) [_placeholderView removeFromSuperview];
            break;
            
        case MOPlaceholderOptionLoading: {
            //加载中
//            _placeholderView.tipLb.text = @"努力加载中...";
            _placeholderView.tipLb.text = @"";
            _placeholderView.imageView.hidden = YES;
            _placeholderView.reloadBtn.hidden = YES;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];
        }
            break;
            
        case MOPlaceholderOptionFail: {
            //加载错误，显示占位图
            _placeholderView.tipLb.text = (@"页面加载开小差了...请刷新");
            [_placeholderView.reloadBtn setTitle:(@"刷新") forState:(UIControlStateNormal)];
            _placeholderView.imageView.image = [UIImage imageNamed:@"placeholder_network_fail"];
            _placeholderView.imageView.hidden = NO;
            _placeholderView.reloadBtn.hidden = NO;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];
//            [XHProgressHud hide];
        }
            break;
            
        case MOPlaceholderOptionNullData: {
            //空数据
            _placeholderView.tipLb.text = (@"暂无数据");
            [_placeholderView.reloadBtn setTitle:(@"刷新") forState:(UIControlStateNormal)];
            _placeholderView.imageView.image = [UIImage imageNamed:@"placeholder_nullData"];
            _placeholderView.imageView.hidden = NO;
            _placeholderView.reloadBtn.hidden = YES;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];

//            [XHProgressHud hide];
        }
            break;
            
        case MOPlaceholderOptionNotReachable: {
            //网络不可达
            _placeholderView.tipLb.text = (@"网络好像走丢了，请检查网络设置");
            [_placeholderView.reloadBtn setTitle:(@"刷新") forState:(UIControlStateNormal)];
            _placeholderView.imageView.image = [UIImage imageNamed:@"placeholder_network_notReachable"];
            _placeholderView.imageView.hidden = NO;
            _placeholderView.reloadBtn.hidden = NO;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];
//            [XHProgressHud hide];
        }
            break;
            
        case MOPlaceholderOptionNeedsLogin: {
            //需要登录
            _placeholderView.tipLb.text = (@"加载页面需要权限，快去登录吧");
            [_placeholderView.reloadBtn setTitle:(@"登录") forState:(UIControlStateNormal)];
            _placeholderView.imageView.image = [UIImage imageNamed:@"placeholder_network_notReachable"];
            _placeholderView.imageView.hidden = NO;
            _placeholderView.reloadBtn.hidden = NO;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];
//            [XHProgressHud hide];
        }
            break;
            
        case MOPlaceholderOptionNotFound: {
            //数据不存在
            _placeholderView.tipLb.text = (@"内容不存在...去其他地方看看吧");
            [_placeholderView.reloadBtn setTitle:(@"刷新") forState:(UIControlStateNormal)];
            _placeholderView.imageView.image = [UIImage imageNamed:@"placeholder_nullData"];
            _placeholderView.imageView.hidden = NO;
            _placeholderView.reloadBtn.hidden = YES;
//            _placeholderView.gifView.hidden   = YES;
            [superView addSubview:_placeholderView];
        }
            break;
            
        default:
            break;
    }
    
//    target.networkingStatus = status;
//    dispatch_semaphore_signal(semaphore);
}



//- (void)updateStatus {
//
//}



#pragma mark -- 刷新 || 登录
- (void)btnAction {
    
    //登录
    if (self.option == MOPlaceholderOptionNeedsLogin) {
        if (self.loginAction) {
            self.loginAction();
        } else {
//            XHLog(@"-------------------------- 未实现登录方法 --------------------------");
            self.option = MOPlaceholderOptionNone;
            if (_placeholderView.superview) [_placeholderView removeFromSuperview];
        }
    }
    
    
    //刷新
    else {
        if (self.refreshAction) {
            self.option = MOPlaceholderOptionPrepare;
            self.refreshAction();
        } else {
//            XHLog(@"-------------------------- 未实现刷新方法 --------------------------");
            self.option = MOPlaceholderOptionNone;
            if (_placeholderView.superview) [_placeholderView removeFromSuperview];
        }
    }
}





#pragma mark -- 约束条件
//- (NSMutableArray *)conditions {
//    if (!_conditions) {
//        _conditions = [NSMutableArray array];
//    }
//    return _conditions;
//}
//
//
////添加
//- (void)addCondition:(id)condition {
//
//    if (condition && ![self.conditions containsObject:condition] && self.isLoading)  {
//        [self.conditions addObject:condition];
//        [self showOption:(MOPlaceholderOptionLoading)];
//    }
//}
//
//
////移除
//- (void)removeCondition:(id)condition {
//
//    if (condition && [self.conditions containsObject:condition])  {
//        [self.conditions removeObject:condition];
//    }
//
////    [self showOption:self.option];
//}
//
//
//- (BOOL)hasCondition {
//    return (BOOL)(self.conditions.count > 0);
//}


@end
